package com.dlwx.parttimetalent.fragment.daren;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.bean.BaseBean;
import com.dlwx.parttimetalent.bean.JobInfoBean;
import com.dlwx.parttimetalent.comm.Constant;
import com.dlwx.parttimetalent.comm.Http;
import com.dlwx.parttimetalent.inter.DialogInter;
import com.dlwx.parttimetalent.ui.EditLabelActivity;
import com.dlwx.parttimetalent.ui.WorkIndustryActivity;
import com.dlwx.parttimetalent.utils.DealResult;
import com.dlwx.parttimetalent.utils.OkgoUtil;
import com.dlwx.parttimetalent.utils.ToastUtils;
import com.dlwx.parttimetalent.views.HouseAreaDialog;
import com.dlwx.parttimetalent.views.MyProgressLoading;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


public class WorkFragment extends Fragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.tv_platform)
    TextView tvPlatform;
    @BindView(R.id.image_nickname)
    ImageView imageNickname;
    @BindView(R.id.tv_attention)
    TextView tvAttention;
    @BindView(R.id.rl_attention)
    RelativeLayout rlAttention;
    @BindView(R.id.et_unit)
    TextView etUnit;
    @BindView(R.id.et_position)
    TextView etPosition;
    @BindView(R.id.et_industry)
    TextView etIndustry;
    @BindView(R.id.et_address)
    TextView etAddress;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    Unbinder unbinder;
    @BindView(R.id.ll_industry)
    LinearLayout llIndustry;

    private String mParam1;
    private String mParam2;

    private MyProgressLoading loadingDialog;

    private OnFragmentInteractionListener mListener;
    private HouseAreaDialog dialog;

    public WorkFragment() {
        // Required empty public constructor
    }

    public static WorkFragment newInstance(String param1, String param2) {
        WorkFragment fragment = new WorkFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        loadingDialog = new MyProgressLoading(getActivity(), R.style.DialogStyle);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_work, container, false);
        unbinder = ButterKnife.bind(this, view);
        getDatas();
        return view;
    }

    private void getDatas() {
//        loadingDialog.show();
        OkgoUtil.post(Http.MyJobInfo, getActivity())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
//                        loadingDialog.dismiss();
                        JobInfoBean parentBean = DealResult.getInstance().dealDatas(getActivity(), response, new TypeToken<JobInfoBean>() {
                        }.getType());
                        if (parentBean == null) {
                            return;
                        }
                        setDatas(parentBean.getBody());
//                        ToastUtils.toastForShort(getActivity(), parentBean.getResult());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
//                        loadingDialog.dismiss();
                        ToastUtils.toastForShort(getActivity(), getString(R.string.bad_net));
                    }
                });
    }

    private void setDatas(JobInfoBean.BodyBean bean) {
        if(bean!=null){
            etUnit.setText(bean.getWorkunit());
            etPosition.setText(bean.getJob_position());
            etIndustry.setText(bean.getJob_class());
            etAddress.setText(bean.getHome_address());
        }
    }


    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
//            throw new RuntimeException(context.toString() + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.rl_attention, R.id.tv_confirm,R.id.ll_industry,R.id.et_unit,R.id.et_position,R.id.et_industry,R.id.et_address})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_attention:
                startActivity(new Intent(getActivity(), EditLabelActivity.class));
                break;
            case R.id.tv_confirm:
                checkInputDatas();
                break;
            case R.id.ll_industry:
                startActivityForResult(new Intent(getActivity(),WorkIndustryActivity.class),0);
                break;
            case R.id.et_unit:
                 dialog=new HouseAreaDialog(getActivity(), new DialogInter() {
                    @Override
                    public void getInfo(String data) {
                        etUnit.setText(data);
                    }

                    @Override
                    public void getWorkInfo(String start, String end, String unit) {

                    }
                },0);
                dialog.show();
                break;
            case R.id.et_position:
                 dialog=new HouseAreaDialog(getActivity(), new DialogInter() {
                    @Override
                    public void getInfo(String data) {
                        etPosition.setText(data);
                    }

                    @Override
                    public void getWorkInfo(String start, String end, String unit) {

                    }
                },1);
                dialog.show();
                break;
            case R.id.et_industry:
                 dialog=new HouseAreaDialog(getActivity(), new DialogInter() {
                    @Override
                    public void getInfo(String data) {
                        etIndustry.setText(data);
                    }

                    @Override
                    public void getWorkInfo(String start, String end, String unit) {

                    }
                },2);
                dialog.show();
                break;
            case R.id.et_address:
                 dialog=new HouseAreaDialog(getActivity(), new DialogInter() {
                    @Override
                    public void getInfo(String data) {
                        etAddress.setText(data);
                    }

                    @Override
                    public void getWorkInfo(String start, String end, String unit) {

                    }
                },3);
                dialog.show();
                break;
        }
    }

    private void checkInputDatas() {
        String unit = etUnit.getText().toString();
        if (TextUtils.isEmpty(unit)) {
            ToastUtils.toastForShort(getActivity(), "住房面积不能为空");
            return;
        }
        String positon = etPosition.getText().toString();
        if (TextUtils.isEmpty(positon)) {
            ToastUtils.toastForShort(getActivity(), "房产信息不能为空");
            return;
        }
        String industry = etIndustry.getText().toString();
        if (TextUtils.isEmpty(industry)) {
            ToastUtils.toastForShort(getActivity(), "月收入不能为空");
            return;
        }
        String address = etAddress.getText().toString();
        if (TextUtils.isEmpty(address)) {
            ToastUtils.toastForShort(getActivity(), "车辆信息不能为空");
            return;
        }
        confirm(unit, positon, industry, address);
    }

    private void confirm(String unit, String positon, String industry, String address) {
        OkgoUtil.post(Http.SubUserJob, getActivity())
                .params("workunit", unit)
                .params("job_position", positon)
                .params("job_class", industry)
                .params("home_address", address)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDialog.dismiss();
                        BaseBean parentBean = DealResult.getInstance().dealDatas(getActivity(), response, new TypeToken<BaseBean>() {
                        }.getType());
                        if (parentBean == null) {
                            return;
                        }
                        if (parentBean.getCode() == Constant.SUCESS) {
                        }
                        ToastUtils.toastForShort(getActivity(), parentBean.getResult());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDialog.dismiss();
                        ToastUtils.toastForShort(getActivity(), getString(R.string.bad_net));
                    }
                });
    }

    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode==Constant.FIVE){
            etIndustry.setText(data.getStringExtra("name"));
        }else{
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(dialog!=null){
            dialog.dismiss();
        }
    }
}
